package hc.j2me.ui;

import hc.core.ContextManager;
import hc.core.IConstant;
import hc.core.RootServerConnector;
import hc.core.util.MsgNotifier;
import hc.j2me.J2MEConstant;
import hc.j2me.J2MEContext;
import hc.j2me.Main;
import hc.j2me.Starter;
import hc.j2me.util.ScreenUtil;

import java.io.IOException;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;

public class LoginForm extends Form {
	TextField uuid = new TextField("", "", 100, TextField.ANY);
	TextField pwd = new TextField("", "", 100, TextField.PASSWORD);
	ChoiceGroup isStorePass = new ChoiceGroup("", ChoiceGroup.MULTIPLE);
	Command optionCmd = new Command(Starter.getUIString("m06"), Command.ITEM, 1);
	Command exitCmd = new Command(Starter.getUIString(ScreenUtil.EXIT), Command.EXIT, 1);
	
	public LoginForm(boolean isFirst) {
		super("HomeCenter");
		
		int w, h;
		
		//非全屏模式下，获得屏幕尺寸
		//供优化参数之用
		w = Starter.config.width;
		h = Starter.config.height;
				
		if(h >= 300 || w >= 300){
			J2MEConstant.SPLIT_MAX_SIZE = 1024;
		}else if(h >= 240 || w >= 240){
			J2MEConstant.SPLIT_MAX_SIZE = 480; 
		}else{
			J2MEConstant.SPLIT_MAX_SIZE = 120;
		}
		
		final int iconSize = Starter.getIconSize(w, h);
		
		if(w >= 160 && h >= 160){
			try {
				append(new ImageItem("", 
						Image.createImage("/hc/j2me/res/mobi_login.png"), 
						Item.LAYOUT_CENTER, ""));
			} catch (Throwable e) {
			}
		}
		
		Image icon = null;
		try {
			icon = Image.createImage("/hc/j2me/res/idsmile_" + iconSize + ".png");
		} catch (IOException e) {
			e.printStackTrace();
		}
//		ImageItem id = new ImageItem("", icon, Item.LAYOUT_LEFT, "");
		append(new ImageItem("", icon, Item.LAYOUT_LEFT | Item.LAYOUT_NEWLINE_BEFORE, ""));
		append(uuid);
		if(isFirst){
			uuid.setString("free ID");
		}else{
			uuid.setString(Starter.config.uuid);
		}
		
		try {
			icon = Image.createImage("/hc/j2me/res/pwd_" + iconSize + ".png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		append(new ImageItem("", icon, Item.LAYOUT_LEFT, ""));
        if(Starter.config.isStorePass){
        	pwd.setString(Starter.config.password);
        }
		append(pwd);
		
		isStorePass.append(Starter.getUIString("1028"), null);
		isStorePass.setSelectedIndex(0, Starter.config.isStorePass);
		append(isStorePass);
		
//		StringItem item = new StringItem("", Starter.getUIString(ScreenUtil.OK), Item.BUTTON);
//		item.setDefaultCommand(Starter.cmd_ok);
//		item.setItemCommandListener(this);
//		append(item);
		
		
//		StringItem item = new StringItem("", Starter.getUIString("m06"), Item.BUTTON);
//		item.setDefaultCommand(optionCmd);
//		item.setItemCommandListener(this);
//		append(item);
		
		
//		item = new StringItem("", Starter.getUIString(ScreenUtil.EXIT), Item.BUTTON);
//		item.setDefaultCommand(Starter.cmd_cancel);
//		item.setItemCommandListener(this);
//		append(item);
		
		append("\n" + Starter.getUIString("m12") + "\n\n" + Starter.getUIString("m15"));
		
		addCommand(exitCmd);
		addCommand(optionCmd);
		addCommand(Starter.cmd_ok);
		
		setCommandListener(new CommandListener() {
			public void commandAction(Command c, Displayable d) {
				if(c == Starter.cmd_ok){
		            IConstant.setUUID(uuid.getString());
		            Starter.config.uuid = uuid.getString();
		        	IConstant.setPassword(pwd.getString());
		        	
		        	final boolean currIs = isStorePass.isSelected(0);
		    		Starter.config.isStorePass = currIs;
		    		if(currIs){
		    			Starter.config.password = pwd.getString();
		    		}else{
		    			Starter.config.password = "";
		    		}
		    		Starter.update(Starter.config);
		        	
		        	Starter.soundOK();
		        	
		        	enterMainUI();
				}else if(c == exitCmd){
					RootServerConnector.notifyLineOffType("lof=MobiLoginExit");
					Main.notifyExit();
				}else if(c == optionCmd){
					ConfigForm.showConfigForm();
				}
			}
		});
		
		//Login Tip
//		if(RootConfig.getInstance().isTrue(RootConfig.p_ShowAutoLoginTip)){
//			try {
//				icon = Image.createImage("/hc/j2me/res/alerm_16.png");
//				append(new ImageItem(null, icon, ImageItem.LAYOUT_NEWLINE_BEFORE, null));
//				
//				//http://www.blogjava.net/myfly/archive/2008/09/01/226172.html
////				Item之后换行的情形：
////				1. StringItem内容后有换行符；
////				2. 具有LAYOUT_NEWLINE_AFTER设定的Item；
////				3. 如果是ChoiceGroup、DateField、Gauge、TextField，而且其布局并未设定为LAYOUT_2。
////				Item之前换行的情形：
////				1. 前一个Item之后有换行；
////				2. 具有LAYOUT_NEWLINE_BEFORE设定的Item；
////				3. StringItem内容开头有换行符；
////				4. 如果是ChoiceGroup、DateField、Gauge、TextField，而且其布局并未设定为LAYOUT_2;
////				5. Item布局设定为LAYOUT_LEFT、LAYOUT_CENTER、LAYOUT_RIGHT。
//				append("Please turn ON automatic logon in Windows or other OS by guest account.\n");
//				append("Because the JVM can NOT capture screen in login window. Mobile will display black even if the server works well. For more, see homepage.");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
	}
	
	
	public void enterMainUI() {		
		ProcessAlert pa = null;
		if(pa == null){			
            ContextManager.setContextInstance(new J2MEContext());          
            ContextManager.start();
            
			pa = new ProcessAlert();
//		}else{
//			pa.clearInfoArea();
		}
		
		MsgNotifier.getInstance().notifyNewMsg("connect server...");

        pa.startThread();
		UIManager.pushInAndSwithToNew(pa);
		
		final ProcessAlert para = pa;
		Thread t = new Thread(){
			public void run(){
				para.connect();
			}
		};
		t.start();

	}
	
	protected void paint(Graphics g) {

	}

}
